package com.knowif.backend.controller;

import com.github.pagehelper.PageInfo;
import com.knowif.backend.service.ProductService;
import com.knowif.pojo.Product;
import com.knowif.pojo.ProductExample;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;
import com.knowif.util.ExampleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：Xh
 * @date ：Created in 2019-12-10 11点19分
 */
@RestController
@RequestMapping("/wechat")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/product/query")
    public RestBean<?> query(@RequestBody SearchCondition searchCondition){
        try {
            ProductExample productExample = ExampleUtil.buildExample(new ProductExample(), searchCondition);
            productExample.setOrderByClause("sell_num DESC");
            List<Product> products = productService.selectProduct(productExample, true);
            PageInfo<Product> pageInfo = new PageInfo<Product>(products);
            return RestBean.build(RestBean.success_code, "", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @GetMapping("/product/{productId}")
    public RestBean<?> detail(@PathVariable("productId") int productId){
        try {
            Product product = productService.selectByPrimaryKey(productId);
            return RestBean.build(RestBean.success_code, "", product);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }
}
